package com.xxl.sso.core.store;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.fastjson.JSON;
import com.xxl.sso.core.api.SysCacheFeignService;
import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.entity.vo.req.SysCacheAddUpdateReqVO;
import com.xxl.sso.core.entity.vo.req.SysCacheKeyCacheValueReqVO;
import com.xxl.sso.core.entity.vo.req.SysCacheKeyReqVO;
import com.xxl.sso.core.entity.vo.res.SysCacheResVO;
import com.xxl.sso.core.user.XxlSsoUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

/**
 * local login store
 *
 * @author xuxueli 2018-04-02 20:03:11
 */
@Slf4j
@Component
public class SsoLoginStore {

    private static SysCacheFeignService sysCacheFeignService;

    @Autowired
    public SsoLoginStore(SysCacheFeignService sysCacheFeignService){
        SsoLoginStore.sysCacheFeignService = sysCacheFeignService;
    }

    /**
     * get
     *
     * @param storeKey
     * @return
     */
    public static XxlSsoUser get(String storeKey) {
        log.info("SsoLoginStore get() enter.param is {}",storeKey);
        String cacheKey = cacheKey(storeKey);
        Object objectValue = null;
        SysCacheKeyReqVO sysCacheKeyReqVO = new SysCacheKeyReqVO();
        sysCacheKeyReqVO.setCacheKey(cacheKey);
        BaseResult<SysCacheResVO> result = sysCacheFeignService.cacheValAqusitionByCacheKey(sysCacheKeyReqVO);
        log.info("SsoLoginStore get() executed.result is {}",result);
        if(!ObjectUtils.isEmpty(result) && !ObjectUtils.isEmpty(result.getData())){
            SysCacheResVO sysCacheResVO = JSON.parseObject(JSON.toJSONString(result.getData()), SysCacheResVO.class);
            objectValue = sysCacheResVO.getCacheValueObj();
        }
        if (objectValue != null) {
            XxlSsoUser xxlUser = JSON.parseObject(JSON.toJSONString(objectValue), XxlSsoUser.class);
            return xxlUser;
        }
        return null;
    }

    /**
     * remove
     *
     * @param storeKey
     */
    public static void remove(String storeKey) {
        log.info("#############SsoLoginStore remove() enter.param is {}",storeKey);
        String cacheKey = cacheKey(storeKey);
        SysCacheKeyReqVO sysCacheKeyReqVO = new SysCacheKeyReqVO();
        sysCacheKeyReqVO.setCacheKey(cacheKey);
        sysCacheFeignService.cacheEvictionByCacheKey(sysCacheKeyReqVO);
    }

    /**
     * put
     *
     * @param storeKey
     * @param xxlUser
     */
    public static void put(String storeKey, XxlSsoUser xxlUser) {
        log.info("########SsoLoginStore put() enter.StoreKey is {},and xxlUser is {}",storeKey,xxlUser);
        String cacheKey = cacheKey(storeKey);
        SysCacheAddUpdateReqVO sysCacheAddUpdateReqVO = new SysCacheAddUpdateReqVO();
        sysCacheAddUpdateReqVO.setCacheKey(cacheKey);
        XxlSsoUser xxlSsoUser = new XxlSsoUser();
        BeanUtils.copyProperties(xxlUser,xxlSsoUser);
        sysCacheAddUpdateReqVO.setXxlSsoUser(xxlSsoUser);
        log.info("#########SsoLoginStore put() cacheRefreshing sysCacheAddUpdateReqVO:{}",sysCacheAddUpdateReqVO);
        BaseResult result = sysCacheFeignService.cacheRefreshing(sysCacheAddUpdateReqVO);
        log.info("#########SsoLoginStore put() cacheRefreshing result:{}",result);
    }

    private static String cacheKey(String sessionId){
        return Conf.SSO_SESSIONID.concat("#").concat(sessionId);
    }

    public static void put(String storeKey, String storeValue) {
        SysCacheKeyCacheValueReqVO sysCacheKeyCacheValueReqVO = new SysCacheKeyCacheValueReqVO();
        sysCacheKeyCacheValueReqVO.setCacheKey(storeKey);
        sysCacheKeyCacheValueReqVO.setCacheValue(storeValue);
        BaseResult result = sysCacheFeignService.put(sysCacheKeyCacheValueReqVO);
        log.info("#########SsoLoginStore put() result:{}",result);
    }

    public static String getStoreValue(String storeKey) {
        BaseResult<String> result = sysCacheFeignService.get(storeKey);
        log.debug("SsoLoginStore getStoreValue result:{}",result);
        if(!ObjectUtils.isEmpty(result)
                && !ObjectUtils.isEmpty(result.getData())){
            return result.getData();
        }
        return null;
    }

    public static void removeByPrefix(String prefix) {
        sysCacheFeignService.removeByPrefix(cacheKey(prefix));
    }
}
